/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/*
common texture slots

generic : 0

sprite : 3
mesh texture : 4
mesh luma : 5
solid sky : 6
alpha sky : 7
sky cubemap : 8
surface texture : 9
lightmaps : 10 to 13
shadowmap : 14
*/

cbuffer cbDrawPerFrame : register(b0) {
	matrix orthoMatrix : packoffset(c0);
	float gamma : packoffset(c4.x);
};

cbuffer cbMainPerFrame : register(b1) {
	matrix WorldMatrix : packoffset(c0);
	float3 forwardVec : packoffset(c4.x);
	float VidWidth : packoffset(c4.w);
	float3 upVec : packoffset(c5.x);
	float VidHeight : packoffset(c5.w);
	float3 rightVec : packoffset(c6.x);
	float particleSize : packoffset(c6.w);
	float WarpTime : packoffset(c7.x);
	float WarpScale : packoffset(c7.y);
	float WarpSpeed : packoffset(c7.z);
	float Gravity : packoffset(c7.w);
	float WarpFactor : packoffset(c8.x);
	float2 SkySpeedScale : packoffset(c8.y);
	float SkyFog : packoffset(c8.w);
	float3 FogRGB : packoffset(c9.x);
	float FogDensity : packoffset(c9.w);
	float3 EyePositionWorld : packoffset(c10.x);

	// this entry is set to the value of the temp1 cvar and can be used for testing various things in shaders
	float TestFloat : packoffset(c10.w);
	matrix InverseViewProj : packoffset(c11);
	matrix ProjectionMatrix : packoffset(c15);
	matrix ViewMatrix : packoffset(c19);
};

cbuffer cbPerObject : register(b2) {
	matrix EntityMatrix : packoffset(c0);
	matrix LocalMatrix : packoffset(c4);
	float lerpBlend : packoffset(c8.x);
	float3 ShadeLight : packoffset(c8.y);
	float3 ShadeVector : packoffset(c9.x);
	float Alpha : packoffset(c9.w);
};

cbuffer cbLightStyles : register(b3) {
	float4 LightStyles[MAX_PACKED_LIGHTSTYLES];
};

cbuffer cbLightProjection : register(b5) {
	matrix FaceMatrix[6] : packoffset(c0);
	float NewLightDepthBias : packoffset(c24.x);
	float LightZTrick : packoffset(c24.y);
	float2 junkjunk : packoffset(c24.z);
};

cbuffer cbLightParameters : register(b6) {
	float3 NewLightOrigin : packoffset(c0.x);
	float NewLightRadius : packoffset(c0.w);
	float3 NewLightColour : packoffset(c1.x);
	float NewLightZPass : packoffset(c1.w);
	float NewLightFalloff : packoffset(c2.x);
	float3 NewSpotVector : packoffset(c2.y);
};

// this is a packed version of the lightstyles array for direct indexing
static float LightStyleValues[MAX_LIGHTSTYLES] = (float[MAX_LIGHTSTYLES]) LightStyles;	

// utility functions
float PS_GetShadeDot (float3 Normal, float3 DirectionVector)
{
	float shadedot = dot (normalize (Normal), normalize (DirectionVector));
	return max (shadedot + 1.0f, (shadedot * 0.2954545f) + 1.0f);
}


// samplers bound to dedicated slots so we only need to bind them once
SamplerState drawSampler : register(s0);
SamplerState surfSampler : register(s1);
SamplerState lmapSampler : register(s2);
SamplerComparisonState smapSampler : register(s4);
SamplerState pointSampler : register(s5);


// common texture slots
Texture2D surfTexture : register(TEXTURE_SLOT_SURFACES);

// common vertex structures
struct SURF_VS
{
	float4 Position : POSITION;
	float2 TexCoord : TEXCOORD;
	float2 Lightmap : LIGHTMAP;
	uint MapNum : MAPNUM;
	int4 Styles : STYLES;
};

struct MESHSV_VS
{
	float4 Position0 : PREVPOSITION;
	float4 Position1 : CURRPOSITION;
};

struct MESH_VS
{
	float4 Position0 : PREVPOSITION;
	float3 Normal0 : PREVNORMAL;
	float4 Position1 : CURRPOSITION;
	float3 Normal1 : CURRNORMAL;
	float2 TexCoord : TEXCOORD;
};


static const float4 QuadPositions[4] = {
	float4 (-1, -1, 0, 1),
	float4 (-1,  1, 0, 1),
	float4 ( 1, -1, 0, 1),
	float4 ( 1,  1, 0, 1)
};


static const float4 IcosPositions[60] = {
	float4 (-0.525731, 0.000000, 0.850651, 1),
	float4 (0.000000, 0.850651, 0.525731, 1),
	float4 (0.525731, 0.000000, 0.850651, 1),
	float4 (-0.525731, 0.000000, 0.850651, 1),
	float4 (-0.850651, 0.525731, 0.000000, 1),
	float4 (0.000000, 0.850651, 0.525731, 1),
	float4 (-0.850651, 0.525731, 0.000000, 1),
	float4 (0.000000, 0.850651, -0.525731, 1),
	float4 (0.000000, 0.850651, 0.525731, 1),
	float4 (0.000000, 0.850651, 0.525731, 1),
	float4 (0.000000, 0.850651, -0.525731, 1),
	float4 (0.850651, 0.525731, 0.000000, 1),
	float4 (0.000000, 0.850651, 0.525731, 1),
	float4 (0.850651, 0.525731, 0.000000, 1),
	float4 (0.525731, 0.000000, 0.850651, 1),
	float4 (0.850651, 0.525731, 0.000000, 1),
	float4 (0.850651, -0.525731, 0.000000, 1),
	float4 (0.525731, 0.000000, 0.850651, 1),
	float4 (0.850651, 0.525731, 0.000000, 1),
	float4 (0.525731, 0.000000, -0.850651, 1),
	float4 (0.850651, -0.525731, 0.000000, 1),
	float4 (0.000000, 0.850651, -0.525731, 1),
	float4 (0.525731, 0.000000, -0.850651, 1),
	float4 (0.850651, 0.525731, 0.000000, 1),
	float4 (0.000000, 0.850651, -0.525731, 1),
	float4 (-0.525731, 0.000000, -0.850651, 1),
	float4 (0.525731, 0.000000, -0.850651, 1),
	float4 (-0.525731, 0.000000, -0.850651, 1),
	float4 (0.000000, -0.850651, -0.525731, 1),
	float4 (0.525731, 0.000000, -0.850651, 1),
	float4 (0.000000, -0.850651, -0.525731, 1),
	float4 (0.850651, -0.525731, 0.000000, 1),
	float4 (0.525731, 0.000000, -0.850651, 1),
	float4 (0.000000, -0.850651, -0.525731, 1),
	float4 (0.000000, -0.850651, 0.525731, 1),
	float4 (0.850651, -0.525731, 0.000000, 1),
	float4 (0.000000, -0.850651, -0.525731, 1),
	float4 (-0.850651, -0.525731, 0.000000, 1),
	float4 (0.000000, -0.850651, 0.525731, 1),
	float4 (-0.850651, -0.525731, 0.000000, 1),
	float4 (-0.525731, 0.000000, 0.850651, 1),
	float4 (0.000000, -0.850651, 0.525731, 1),
	float4 (-0.525731, 0.000000, 0.850651, 1),
	float4 (0.525731, 0.000000, 0.850651, 1),
	float4 (0.000000, -0.850651, 0.525731, 1),
	float4 (0.000000, -0.850651, 0.525731, 1),
	float4 (0.525731, 0.000000, 0.850651, 1),
	float4 (0.850651, -0.525731, 0.000000, 1),
	float4 (-0.850651, 0.525731, 0.000000, 1),
	float4 (-0.525731, 0.000000, 0.850651, 1),
	float4 (-0.850651, -0.525731, 0.000000, 1),
	float4 (-0.850651, 0.525731, 0.000000, 1),
	float4 (-0.850651, -0.525731, 0.000000, 1),
	float4 (-0.525731, 0.000000, -0.850651, 1),
	float4 (-0.850651, 0.525731, 0.000000, 1),
	float4 (-0.525731, 0.000000, -0.850651, 1),
	float4 (0.000000, 0.850651, -0.525731, 1),
	float4 (0.000000, -0.850651, -0.525731, 1),
	float4 (-0.525731, 0.000000, -0.850651, 1),
	float4 (-0.850651, -0.525731, 0.000000, 1)
};

